<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\View;
use think\Cookie;
use think\Model;
use app\index\model\AdminModel;
use app\index\model\IndexModel;
class Admin extends Controller{
	public function index(){
		$view=new View();
		return $view->fetch();
	}
//	public function admin_login(){
//		$view = new View();
//		if (empty($_POST['name'])) {
//			$this->error('还没登录，请先登录','Admin/index');
//		}
//			$name = $_POST['name'];
//			$password = md5($_POST['password']);
//			$where=array(
//				'name'=>$name,
//				'password'=>$password,
//				'power'=>'1'
//			);
//			$admin=Db::name("jtsc_admin")->where($where)->find();
//			if (!empty($admin)) {
//				
//				$m=new AdminModel;
//				$powers=$m->getPowers();
//				$view->assign('powers',$powers);
//					
//				Cookie::set('jtsc_power', $admin['power'], 21600);				
//					
//				//修改最后一次登录时间
//				$date = strtotime("now");
//				$data = [
//						'last_time' => $date
//				];
//				Db::name('jtsc_admin')->where('id', $admin['id'])->update($data);
//				/********************
//				4 1、写入内容到日志文件
//				6 ********************/
//				$action="管理员登录";
//				$datalog=[
//						'log_time'=>$date,
//						'log_uid'=>Cookie::get('user_id'),
//						'log_ip'=>$_SERVER["REMOTE_ADDR"],
//						'log_url'=>$_SERVER["REQUEST_URI"],
//						'log_action'=>$action
//				];
//				Db::name('waimai_log')->insert($datalog);
//				$this->redirect('Index/neworder_list');
////	    			$this->neworder_list();
//
//			} else {
//				$view->assign('str', '用户名或密码错误');
//				return $view->fetch('index');
//			}
//	}
	
	public function user_admin(){
		$view=new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
		//取出所有用户
		$staffs_list=Db::name('merchant_store_staff')->select();
		foreach($staffs_list as $key=>$f){
			$fid=$f['id'];
			$power=Db::name('jtsc_admin')->where('uid',$fid)->find();
			
			if(!empty($power)){
				//将权利字符串转为数组
				$power_array=explode(",",$power['power']);
				if(!empty($power_array)){
					foreach($power_array as $p){
						//根据pid取出该权利
						$power_name=Db::name('jtsc_power')->where('pid',$p)->find();
						$staffs_list[$key]['power'][$p]=$power_name;
					}
				}
			}
		}
		$view->assign('staffs_list',$staffs_list);
		return $view->fetch();
	}
	public function update_power($uid){
		$view=new View();
		$id=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($id);
		$view->assign('powers',$powers);
		//取出所有用户
		$staff=Db::name('merchant_store_staff')->where('id',$uid)->find();
		if(empty($staff)){
			$this->error("该用户不存在");exit;
		}
		
			$power=Db::name('jtsc_admin')->where('uid',$uid)->find();
			if(!empty($power)){
				//将权利字符串转为数组
				$power_array=explode(",",$power['power']);
				if(!empty($power_array)){
					foreach($power_array as $p){
						//根据pid取出该权利
						$power_name=Db::name('jtsc_power')->where('pid',$p)->find();
						$staff['power'][$p]=$power_name;
						$admin[$power_name['name']]=1;
						
					}
				}
			}
			$where=array(
				'parent_id'=>1
			);
		$powers_list=Db::name("jtsc_power")->where($where)->select();
		$view->assign("admin",$admin);
		$view->assign('staff',$staff);
		$view->assign('powers_list',$powers_list);
		return $view->fetch();
	}
	public function getuser($uid){
		$m=new AdminModel;
		$view=new View();
		$user_id=Cookie::get('user_id');
		if(empty($user_id)){
			$this->error("请登录");exit;
		}
		$flag=$m->checkAdmin($user_id);
		if($flag==0){
			$this->error("您不是超级管理员，无法查看");exit;
		}
		$user=Db::name('merchant_store_staff')->where('id',$uid)->find();
		$store=Db::name('merchant_store')->where('store_id',$user['store_id'])->find();
		$view->assign("staff",$user);
		$view->assign("store",$store);
		return $view->fetch();
	}
	public function update_power2(){
		
		$view=new View();
		$id=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($id);
		$view->assign('powers',$powers);
		$uid=$_POST['uid'];
		$pids=$_POST['pids'];
		$pidstr=implode(",",$pids);
		//取出所有用户
		$staff=Db::name('merchant_store_staff')->where('id',$uid)->find();
		if(empty($staff)){
			$this->error("该用户不存在");exit;
		}
		$where=array(
			'uid'=>$uid
		);
		$data=[
			'power'=>$pidstr
		];
		Db::name("jtsc_admin")->where($where)->update($data);
		
		$this->redirect("Admin/user_admin");
	}
	public function addstaff(){
		$view=new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$where=array(
				'parent_id'=>1
			);
		$powers_list=Db::name("jtsc_power")->where($where)->select();
		
		$store_list=Db::name("merchant_store")->select();
		$view->assign('store_list',$store_list);
		$view->assign('powers_list',$powers_list);
		return $view->fetch();
	}
	public function addstaff2(){
		$view=new View();
		$uid=Cookie::get("user_id");
		$m=new IndexModel;
		$powers=$m->getPowersByUid($uid);
		$view->assign('powers',$powers);
				
		$name=$_POST['name'];
		$password=md5($_POST['password']);
		$store_id=$_POST['store_id'];
		if(empty($store_id)){
			$store_id=114;
		}
		$pids=$_POST['pids'];
		$pidstr=implode(",",$pids);
		$time=strtotime("now");
		$datau=[
			'store_id'=>$store_id,
			'token'=>46,
			'username'=>$name,
			'password'=>$password,
			'time'=>$time,
			'last_time'=>$time
		];
		
		Db::name("merchant_store_staff")->insert($datau);
		$uid=Db::name("merchant_store_staff")->field('id')->where('time',$time)->find();
		$data_admin=[
			'uid'=>$uid['id'],
			'store_id'=>$store_id,
			'mer_id'=>46,
			'add_time'=>$time,
			'power'=>$pidstr
		];
		Db::name("jtsc_admin")->insert($data_admin);
		
		$this->redirect("Admin/user_admin");
	}
	public function uexit()
    {
    	 $view = new View();
    	 cookie::delete('user_id');
    	 cookie::delete('user_name');
    	 cookie::delete('user_password');
    	 cookie::delete('store_id');
    	 cookie::delete('store_name');

    	return $view->fetch('index');
    	
      
    }
}
?>